package com.zt.globalExceptionHandler;

import com.zt.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;


@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        String message = e.getCause().getMessage();
            String[] split = message.split(" ");
            String msg = split[2] + "已存在";
            return Result.error(msg);
    }
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        return Result.error("服务器出错，请联系管理员");
    }
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Result handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        return Result.success("不能上传大于1M的图片");
    }
    @ExceptionHandler(BizException.class)
    public Result handleBizException(BizException e) {
        return Result.error(e.getMessage());
    }
}
